package au.com.handsunited.web.taglib.relationship;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import au.com.handsunited.model.BaseEntity;
import au.com.handsunited.model.relationship.Relationship;
import au.com.handsunited.model.relationship.RelationshipStatus;


/**
 * @author arunda
 *
 */
public class RelationshipStatusTag extends BodyTagSupport {

	private static Logger logger = LoggerFactory.getLogger(RelationshipStatusTag.class);

	private String status;
	private RelationshipTag parent;
	private Relationship relationship;

	/**
	 * @throws JspException
	 *
	 */
	public RelationshipStatusTag() throws JspException {
	}


	/**
	 * @see javax.servlet.jsp.tagext.BodyTagSupport#doStartTag()
	 */
	@Override
	public int doStartTag() throws JspException {
		if ((getRelationship() == null) && status.equalsIgnoreCase(RelationshipStatus.UNKNOWN.getName())) {
			return EVAL_BODY_INCLUDE;
		} else if ((getRelationship() != null) && getRelationship().getStatus().getName().equalsIgnoreCase(status)) {
			return EVAL_BODY_INCLUDE;
		} else {
			return SKIP_BODY;
		}

	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public BaseEntity getEntity() throws JspTagException {

		if (parent == null) {
			parent = (RelationshipTag) findAncestorWithClass(this, RelationshipTag.class);
			if (parent == null) {
				throw new JspTagException("status not inside relationship");
			}
		}

		return parent.getEntity();
	}

	public BaseEntity getMember() throws JspTagException {

		if (parent == null) {
			parent = (RelationshipTag) findAncestorWithClass(this, RelationshipTag.class);
			if (parent == null) {
				throw new JspTagException("status not inside relationship");
			}
		}

		return parent.getMember();
	}


	public Relationship getRelationship() throws JspException {

		if (relationship == null) {
			RelationshipTag parent = (RelationshipTag) findAncestorWithClass(this, RelationshipTag.class);
			if (parent == null) {
				throw new JspTagException("status not inside relationship");
			}
			Relationship relationship = parent.getRelationship();
		}

		return relationship;
	}


	public void setRelationship(Relationship relationship) {
		this.relationship = relationship;
	}



}
